import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Checkbox;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import javax.swing.JCheckBox;
import javax.swing.*;
import javax.swing.border.BevelBorder;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BiomarkerUI extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JCheckBox nPatient;
	private JTextArea bioDisplay;
	private JButton enterRes;
	private JButton submit;
	private JScrollPane scrollPane;
	private JTextField dField;
	private JLabel dLabel;
	private JLabel nLabel;
	private JTextField lName;
	private JTextField fName;
	public String diagnosis="";

	/**
	* Auto-generated main method to display this JFrame
	*/
//	public boolean getStatus(JCheckBox x){
//		boolean work = x.isSelected();
//		return work;
//	}
	
	public BiomarkerUI() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("View Biomarkers");
			{
				nPatient = new JCheckBox();
				nPatient.setText("New Patient?");
			}
			{
				fName = new JTextField();
				fName.setText("First Name");
			}
			{
				lName = new JTextField();
				lName.setText("Last Name");
			}
			{
				nLabel = new JLabel();
				nLabel.setText("Patient Name");
			}
			{
				dLabel = new JLabel();
				dLabel.setText("Diagnosis");
			}
			{
				bioDisplay = new JTextArea("Relevant biomarkers:", 1000,3);
				bioDisplay.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				scrollPane = new JScrollPane(bioDisplay);
				bioDisplay.setEditable(false);
			}
			{
				dField = new JTextField();
				dField.setText("Cancer Type");
			}
			{
				submit = new JButton();
				submit.setText("Submit");
				submit.addActionListener(new ButtonListener());
			}
			{
				enterRes = new JButton();
				enterRes.setText("Enter Test Results");
				enterRes.addActionListener(new ButtonListener());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(fName, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(nPatient, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
				    .addComponent(lName, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(nLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(dLabel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(dField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(submit, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(enterRes, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(scrollPane, 0, 160, Short.MAX_VALUE)
				.addContainerGap(18, 18));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(dField, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(submit, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
				        .addGap(18)
				        .addComponent(enterRes, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(nLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
				            .addComponent(dLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(fName, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
				        .addComponent(lName, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(nPatient, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 366, GroupLayout.PREFERRED_SIZE)))
				.addContainerGap(12, Short.MAX_VALUE));
			thisLayout.linkSize(SwingConstants.VERTICAL, new Component[] {enterRes, submit});
			thisLayout.linkSize(SwingConstants.VERTICAL, new Component[] {dLabel, nLabel});
			thisLayout.linkSize(SwingConstants.VERTICAL, new Component[] {fName, lName});
			thisLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {dLabel, nLabel});
			thisLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {lName, fName});
			pack();
			this.setSize(406, 327);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	private class ButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource()==submit){
				diagnosis = dField.getText();
				try { //this try/catch establishes connection and passes sql query, and the idea is to print the retun
					String connectURL = "jdbc:mysql://141.225.166.202:3306/comp4081"; // test is the name of the database            
					Connection connection = null;// declare a connection by using Connection interface             
					Statement statement = null;// declare object of Statement interface that uses for executing sql statements.            
					//ResultSet rs = null;// declars resultset for output data from the table.
					//int updateQuery = 0;
					String status = "";
					status = "connecting...\n";				
					System.out.println(status);
					Class.forName("com.mysql.jdbc.Driver").newInstance();// Load JBBC driver "com.mysql.jdbc.Driver".            
					connection = DriverManager.getConnection(connectURL, "root", "cancer11");// Create a connection by using getConnection()
					status = status + "...connected\n";				
					System.out.println(status);
					/* createStatement() is used for create statement object that is used for sending sql statements
		            to the specified database. */
					
					status = status + "creating statement\n";			
					System.out.println(status);
					
					statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
	                        ResultSet.CONCUR_UPDATABLE);
					
					status = status + "created statement\n";				
					System.out.println(status);
		            
		            status = status + "sending query\n";				
					System.out.println(status);
					//query biomarker
					ResultSet rs = statement.executeQuery("SELECT * from BIOMARKER where CTYPE = '" + diagnosis + "'");
					status = status + "query sent\n";
		            System.out.println(status);
		            
		            LinkedList<Biomarker> bioSet = new LinkedList<Biomarker>();
					String result = "";
					while (rs.next()) {
						bioSet.add(new Biomarker(rs.getInt(1), rs.getString(2)));
					}
					for(int i=0; i < bioSet.size(); i++){
						result += bioSet.get(i).toString() + "\n";
					}
					bioDisplay.setText(result);
					rs.close();
					//end of query biomarker
					
					//insert patient, if new
					if(nPatient.isSelected()){
						statement.executeUpdate("Insert into PATIENT(FIRST_NAME, LAST_NAME) VALUES ('" + fName.getText() + "', '" + lName.getText() + "')");
					}
					
					//query Patiend ID
					ResultSet rsPID = statement.executeQuery("SELECT * from PATIENT where FIRST_NAME = '" + fName.getText() + "' and LAST_NAME = '" + lName.getText() + "'");
					rsPID.first();
					System.out.println(rsPID.getInt(1));
					rsPID.close();
					
		            
					statement.close();
					connection.close();
				} catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
					bioDisplay.setText("Error with database.");
					System.out.println(ex.getMessage());				
				}
			}
			else{
				System.exit(0);
			}
		
		}//end actionPerformed
	 
	}//end class ButtonListener

}
